package controller

import (
	"fx_swift_forex/internal/service"
	"fx_swift_forex/pkg/code"
	"fx_swift_forex/pkg/global"
	"fx_swift_forex/pkg/internalerrors"
	"fx_swift_forex/pkg/upload"
	"github.com/gin-gonic/gin"
	"net/http"
)

func GetForexCountryInfo(c *gin.Context) {
	result, err := service.GetForexCountryInfo(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 文件上传
func UploadComplyFiles(c *gin.Context) {
	var (
		result []string
		err    error
	)

	s3OrAliYun := global.ViperObj.GetString("comply.uploadFlag")
	if s3OrAliYun == "s3" {
		result, err = upload.UploadFile(c)
	} else {
		result, err = upload.BatchUploadFile(c)
	}

	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	}
	if result == nil {
		return
	}

	code.Success(c, gin.H{
		"files": result,
	})
}

// 获取省份
func GetProvinceInfo(c *gin.Context) {
	result, err := service.GetProvinceInfo(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取城市
func GetCityInfo(c *gin.Context) {
	countrySubdivision := c.Query("countrySubdivision")
	result, err := service.GetCityInfo(c, countrySubdivision)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取公司类型
func GetCompanyType(c *gin.Context) {
	result, err := service.GetCompanyType(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取业务类型
func GetBusinessType(c *gin.Context) {
	result, err := service.GetBusinessType(c)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 获取(US，CA，MX)省州的二字码
func GetStateOrProvinceInfo(c *gin.Context) {
	isoCountryCode := c.Query("isoCountryCode")
	result, err := service.GetStateOrProvinceInfo(c, isoCountryCode)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}
